import com.sun.org.apache.xalan.internal.xsltc.compiler.util.CompareGenerator;
import sun.net.www.content.text.Generic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2023-04-07
 * Time: 1:22
 */
public class Solution2 {
    public ListNode sortInList (ListNode head) {
        //使用数组存储链表元素，然后排序，重新构造链表
        ArrayList<Integer> list=new ArrayList<>();
        ListNode cur=head;
        while (cur!=null){
            list.add(cur.val);
            cur=cur.next;
        }
        //重新构造链表的值
        cur=head;
        //默认为升序排序
        Collections.sort(list);
        //遍历数组构造链表,改变值而已
        for (Integer integer : list) {
            cur.val = integer;
            cur = cur.next;
        }
        return head;
    }
}
